<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台管理</title>
    <link rel="icon" href="${pageContext.request.contextPath}/lib/static/login/images/favicon.ico">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/lib/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/lib/static/common/js/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/lib/static/common/js/jquery.form.js"></script>
    <script src="${pageContext.request.contextPath}/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-sm6 layui-col-sm-offset3">
            <div class="layui-fluid">
                <hr class="layui-bg-red">
                <form id="roleForm" class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <input type="hidden" id="id" name="id">
                        <label class="layui-form-label">角色名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="layui-roleName" name="roleName" autofocus
                                   lay-verify="required|roleName" lay-verType="tips" placeholder="请输入角色名称"
                                   autocomplete="off" id="layui-roleName"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色描述</label>
                        <div class="layui-input-block">
                            <input type="text" id="layui-roleDesc" name="roleDesc" lay-verify="required"
                                   lay-verType="tips" placeholder="请输入角色描述"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>

                <%--    <div class="layui-form-item">
                        <label class="layui-form-label">是否授权</label>
                        <div class="layui-input-block">
                            <input type="checkbox" id="grantEnable" title="是否禁用" name=" " lay-skin="switch"
                                   lay-text="启用|禁用" value="已授权">
                        </div>
                    </div>--%>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="saveRole">保存</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    var baseUrl = '${pageContext.request.contextPath}/sys/role/';

    /*发送ajax请求访问后台*/
    function sendAjaxPostReq($, param, method) {
        $.post(
            baseUrl + method,
            param,
            function (result) {
                if (result.code == 0) {
                    layer.msg(result.msg, {icon: 1, time: 500}, function () {
                        parent.window.location.reload();
                    });
                } else {
                    layer.msg(result.msg, {icon: 5, anim: 6});
                }
            },
            "json"
        );
    }
    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.$;
        //自定义验证规则
        form.verify({
            roleName: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '用户名不能全为数字';
                }
            },
            password: [/(.+){6,12}$/, '密码必须6到12位']
        });
        /*表单提交*/
        form.on('submit(saveRole)', function (data) {
            sendAjaxPostReq($, data.field, "getUpdate");
            return false;
        });
    });
</script>
</form>
</div>
</body>
</div>
</html>
